package com.medicine.hbp.fragment.advice;

import org.kymjs.kjframe.http.HttpParams;

import com.medicine.hbp.adapter.FeedbackAdapter;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.FeedbackInfoVo;
import com.medicine.hbp.mob.FeedbackVo;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;
import com.medicine.hbp.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

/**
 * 用户反馈
 *
 * @author yusuojindan
 */
public class FeedbackFragment extends Fragment implements IXListViewListener {
	private XListView xListView_feedback;
	private FeedbackAdapter adapter;
	private Button btn_feedback;
	private EditText edit_feedback;
	private volatile int page = 1;// 当前页（从0开始）
	private int pageSize = 100;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_feedback, null);
		xListView_feedback = (XListView) view.findViewById(R.id.xListView_feedback);
		btn_feedback= (Button) view.findViewById(R.id.btn_feedback);
		edit_feedback= (EditText) view.findViewById(R.id.edit_feedback);
		iniView();
		return view;
	}

	private void iniView() {
		adapter = new FeedbackAdapter(getActivity());
		xListView_feedback.setAdapter(adapter);
		xListView_feedback.setXListViewListener(this);
		xListView_feedback.setPullLoadEnable(false);
		btn_feedback.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				httpAddOpinion();
			}
		});
		httpGetOpinion();
	}

	/**
	 * 意见反馈发送
	 */
	private void httpAddOpinion() {
		final String edit=edit_feedback.getText().toString();
		if(TextUtils.isEmpty(edit)){
			ToastUtil.showShort(getActivity(), "您还没有填写意见哦！");
			return;
		}
		HttpParams params = new HttpParams();
		params.put("msg", edit);
		params.put("otherUserId", LoginSp.getUserId(getActivity()));
		AppHttps.HttpPost(getActivity(),HttpInterface.ADD_OPINION, params,
				new AppHttpCallBack<Object>(Object.class) {
					@Override
					public void onSuccess(Object result) {
						FeedbackInfoVo items=new FeedbackInfoVo();
						items.opinionDate=CommUtil.formatDate(System.currentTimeMillis());
						items.opinionMsg=edit;
						items.userId=LoginSp.getUserId(getActivity());
						adapter.addItem(items);
						edit_feedback.setText("");
						xListView_feedback.setSelection(xListView_feedback.getBottom());
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
				});

	}

	/**
	 * 意见反馈数据获取
	 */
	private void httpGetOpinion() {
		HttpParams params = new HttpParams();
		params.put("current", page);
		params.put("rowCount", pageSize);
		AppHttps.HttpPost(getActivity(),HttpInterface.GET_OPINION, params,
				new AppHttpCallBack<FeedbackVo>(FeedbackVo.class, getActivity()) {
					@Override
					public void onSuccess(FeedbackVo result) {
						boolean isbo=true;
						if(page==1){
							adapter.setItems(result.info);
							xListView_feedback.setSelection(xListView_feedback.getCount()-1);
						}else{
							adapter.addItems(result.info);
						}
						adapter.notifyDataSetChanged();
						if(result.info.size()<100){
							isbo=false;
						}
						xListView_feedback.setPullRefreshEnable(isbo);
						if(page==1){
							xListView_feedback.setSelection(xListView_feedback.getBottom());
						}
					}
					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
					@Override
					public void onFinish() {
						super.onFinish();
						xListView_feedback.setRefreshTime(CommUtil.formatDate(System.currentTimeMillis()));
					}
				});
	}

	@Override
	public void onRefresh() {
		page++;
//		httpGetOpinion();
		xListView_feedback.setRefreshTime(CommUtil.formatDate(System.currentTimeMillis()));
	}

	@Override
	public void onLoadMore() {
//		httpGetOpinion();
	}

}
